package com.zonski.jbots.game.entity;

import com.zonski.jbots.engine.Entity;
import com.zonski.jbots.engine.Engine;
import com.zonski.jbots.engine.CollisionData;
import com.zonski.jbots.engine.QuickVector;
import com.zonski.jbots.engine.entity.Mind;
import com.zonski.jbots.game.JBotsConstants;
import com.zonski.jbots.game.EntityHelper;

import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 20, 2003
 * Time: 3:04:57 PM
 * To change this template use Options | File Templates.
 */
public class ConverterBotMind extends SeekerBotMind
{
    public ConverterBotMind()
    {
    }

    public int getDesirability(QuickVector entities, int distance, int direction)
    {
        int desirability;
        if(distance == 1)
        {
            if(direction == this.getEntity().direction)
            {
                desirability = 45;
            }else{
                desirability = 40 + this.random.nextInt()%7;
            }
        }else{
            desirability = 0;
        }
        if(entities != null)
        {
            for(int i=entities.size(); i > 0; )
            {
                i--;
                Entity entity = (Entity)entities.elementAt(i);
                if(entity.layerId != this.getEntity().layerId)
                {
                    if(entity.entityType.getId() == JBotsConstants.EGG_ID)
                    {
                        desirability += (14 - distance);
                    }else if(entity.entityType.getId() == JBotsConstants.BOT_ID){
                        // crash into bosses
                        desirability += (7 - distance/2);
                    }else if(JBotsConstants.isBot(entity.entityType.getId())){
                        if(distance > 4)
                        {
                            desirability -= (14 - distance);
                        }else{
                            // attack!!!
                            desirability += 7 - distance;
                        }
                    }else if(entity.entityType.getId() == JBotsConstants.BOMB_ID){
                        desirability += (12 - distance);
                    }
                }else if(entity.entityType.getId() == JBotsConstants.CONVERTER_BOT_ID){
                    desirability -= (14 - distance);
                }else if(entity.entityType.getId() == JBotsConstants.BOMB_ID){
                    if(distance < 2)
                    {
                        desirability -= 50;
                    }else if(distance == 2){
                        desirability -= 1;
                    }
                }
            }
        }
        return desirability;
    }

    public void collision(Engine engine, CollisionData data)
    {
        super.collision(engine, data);
        // convert the eggs it runs over
        if(data.with.layerId != this.getEntity().layerId &&
                (data.with.entityType.getId() == JBotsConstants.EGG_ID) ||
                (data.with.entityType.getId() != JBotsConstants.BOT_ID &&
                JBotsConstants.isBot(data.with.entityType.getId())))
        {
            // make it yours
            engine.getRoom().removeEntity(data.with);
            data.with.layerId = this.getEntity().layerId;
            engine.getRoom().addEntity(data.with);
            if(JBotsConstants.isBot(data.with.entityType.getId()))
            {
                // we die as a result of this
                this.getEntity().setRemovable(true);
            }
        }else if(data.with.layerId != this.getEntity().layerId &&
                data.with.entityType.getId() == JBotsConstants.BOMB_ID){
            // turn it into a bomb bot
            data.with.setRemovable(true);
            try
            {
                EntityHelper.addEntity(JBotsConstants.BOMB_BOT, data.with, engine, this.getEntity().layerId);
            }catch(Exception ex){
                System.err.println("couldn't convert a bomb to bomb bot");
            }
        }
    }

    public Mind copy()
    {
        return new ConverterBotMind();
    }
}
